package com.company.cloud.tenant.client;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.tenant.modules.api.request.*;
import com.company.cloud.tenant.modules.api.response.TenantFull;
import com.company.cloud.tenant.modules.api.response.TenantFullPage;
import com.company.cloud.tenant.modules.api.response.TenantSimple;
import com.company.cloud.tenant.modules.api.service.TenantRemoteService;
import com.company.cloud.tenant.modules.cache.service.TenantCacheService;
import com.company.cloud.tenant.modules.cache.utils.TenantConvertUtils;
import com.company.cloud.tenant.modules.mgr.service.TenantMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
@Service
public class TenantRemoteServiceImpl implements TenantRemoteService {

    @Autowired
    private TenantMgrService tenantMgrService;
    @Autowired
    private TenantCacheService tenantCacheService;

    @Override
    public BaseClientResult<TenantSimple> getTenantSimple(String tenantCode) {
        try {
            TenantFull entity= tenantCacheService.getTenantByDB(tenantCode);
            return BaseClientResult.success(TenantConvertUtils.getTenantSimple(entity));
        }catch (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<Map<String, TenantSimple>> getTenantMapSimple(Set<String> tenantCodes) {
        try {
            Map<String, TenantSimple> data=new HashMap<String, TenantSimple>();
            Map<String, TenantFull> companyMap= tenantCacheService.getTenantMapByCache(tenantCodes);
            if(!CollectionUtils.isEmpty(companyMap)) {
                companyMap.forEach((k,v)->{
                    data.put(k, TenantConvertUtils.getTenantSimple(v));
                });
            }
            return BaseClientResult.success(data);
        }catch (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<TenantFull> getTenantFull(String tenantCode) {
        try {
            TenantFull entity= tenantCacheService.getTenantByDB(tenantCode);
            return BaseClientResult.success(entity);
        }catch (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<Map<String, TenantFull>> getTenantMapFull(Set<String> tenantCodes) {
        try {
            Map<String, TenantFull> data= tenantCacheService.getTenantMapByDB(tenantCodes);
            return BaseClientResult.success(data);
        }catch (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<TenantFullPage> queryTenantFull(TenantQueryRequest request) {
        try {
            IPage<TenantFull> data=tenantMgrService.query(request);
            TenantFullPage result=new TenantFullPage();
            result.setRecords(data.getRecords());
            result.setCurrent(data.getCurrent());
            result.setTotal(data.getTotal());
            result.setSize(data.getSize());
            result.setPages(data.getPages());
            return BaseClientResult.success(result);
        }catch  (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<TenantFull> addTenant(TenantAddRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            TenantFull data= tenantMgrService.save(request,userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<TenantFull> addTenant2User(Tenant2UserAddRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());

            if(StringUtils.isEmpty(request.getOpIp())){
                userInfo.setIp("127.0.0.1");
            }else{
                userInfo.setIp(request.getOpIp());
            }
            TenantFull data=tenantMgrService.save2user(request,userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<TenantFull> updateTenant(TenantUpdateRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            TenantFull data=tenantMgrService.update(request,userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<TenantFull> delTenant(TenantDelRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            TenantFull data=tenantMgrService.del(request.getCompanyNo(),userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<TenantFull> checkTenant(TenantCheckRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            TenantFull data=tenantMgrService.check(request,userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<TenantFull> frozenTenant(TenantFrozenRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            TenantFull data=tenantMgrService.frozen(request.getCompanyNo(),userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<TenantFull> unfreezeTenant(TenantUnFreezeRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            TenantFull data=tenantMgrService.unfreeze(request.getCompanyNo(),userInfo);
            return BaseClientResult.success(data);
        }catch  (Exception e){
            e.printStackTrace();
            return  BaseClientResult.error(e);
        }
    }
}
